#include "graphmonthly.h"
#include "ui_graphmonthly.h"
#include<QDesktopWidget>
#include<QMessageBox>
//#include<QFile>
#include<QDebug>
//#include<QTextStream>
#include<QtSql/QSqlQuery>
//This shows monthly graphical view of expenses
GraphMonthly::GraphMonthly(int mm,int yy,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::GraphMonthly)
{
    ui->setupUi(this);
    month=mm;
    year=yy;
    /*QFile f("C:\\Data\\log1.txt");

    if(f.open(QIODevice::Append))
    {
        QTextStream t(&f);
        t<<"In monthly";
    }*/
    QAction* backAction = new QAction( tr("Back"), this );
    backAction->setSoftKeyRole( QAction::NegativeSoftKey );
    connect(backAction, SIGNAL(triggered()), this, SLOT(back()));
    addAction( backAction );

    QRect q=QApplication::desktop()->availableGeometry(this);

    int margin=0,Awidth=0;
    Awidth=q.width();
    margin=q.width()/12.0;//For 240 width ,it is 20
    if(q.width()>500)
        margin=q.width()/18.0;

    int fontSize=6;
    /*if(q.width()>500)
        fontSize=7;
    else
        fontSize=margin/4.0;*/
    qDebug()<<"Width"<<q.width()<<"Height"<<q.height();
    qDebug()<<"FontSize"<<fontSize;
    ui->Vline->setGeometry(2.0*margin,margin,margin,(q.height()-(3*margin)));//Vertical
   qDebug()<<margin;
    ui->Hline->setGeometry((2.5*margin),(q.height()-(2.0*margin)),((q.width())-(3.0*margin)),margin/10.0);//Horizontal
    int avWidth=(Awidth-(ui->Vline->width()*4.0));//For 240, it is 160 available width
    int space=avWidth/40.0;//For 160 available width, it is 4
    int size=(avWidth/6.0)-space;
    int labelSize=1.7*size;
    if(q.width()>500)
        labelSize=1.0*size;
    ui->label_header->setFont(QFont( "lucida", 1.7*fontSize, QFont::Normal, TRUE));
    ui->label_header->setGeometry(1.5*margin,q.height()/2.0,q.width(),margin);
    ui->label_header->setVisible(false);
    int width=(ui->Vline->width()*3.0);

    ui->labelX->setAlignment(Qt::AlignRight);
    ui->labelX->setGeometry(0,(q.height()-(1.6*margin)),(2.5*margin),size);
    ui->labelX->setFont((QFont( "lucida", fontSize, QFont::Normal, TRUE )));
    ui->labelX->setText("Week");

    ui->progressBar1->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label1->setGeometry(width,(q.height()-(1.8*margin)),size,size);
    ui->label1->setAlignment(Qt::AlignTop);
    ui->label1->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));


    width=width+ui->progressBar1->width()+space;
    ui->progressBar2->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label2->setAlignment(Qt::AlignTop);
    ui->label2->setGeometry(width,(q.height()-(1.8*margin)),size,size);
    ui->label2->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    width=width+ui->progressBar2->width()+space;
    ui->progressBar3->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label3->setGeometry(width,(q.height()-(1.8*margin)),size,size);
    ui->label3->setAlignment(Qt::AlignTop);
    ui->label3->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    width=width+ui->progressBar3->width()+space;
    ui->progressBar4->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label4->setGeometry(width,(q.height()-(1.8*margin)),size,size);
    ui->label4->setAlignment(Qt::AlignTop);
    ui->label4->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    width=width+ui->progressBar4->width()+space;
    ui->progressBar5->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label5->setGeometry(width,(q.height()-(1.8*margin)),size,size);
    ui->label5->setAlignment(Qt::AlignTop);
    ui->label5->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

    width=width+ui->progressBar5->width()+space;
    ui->progressBar6->setGeometry(width,margin,size,(ui->Vline->height()));
    ui->label6->setGeometry(width,(q.height()-(1.8*margin)),size,size);
    ui->label6->setAlignment(Qt::AlignTop);
    ui->label6->setFont(QFont( "lucida", fontSize, QFont::Normal, TRUE));

   /* width=width+ui->progressBar6->width();
    ui->labelXT->setAlignment(Qt::AlignTop);
    ui->labelXT->setFont((QFont( "lucida", fontSize, QFont::Normal, TRUE )));
     ui->labelXT->setGeometry(width,(q.height()-(1.6*margin)),size,size);
     ui->labelXT->setText("X");*/

    ui->Vlabel1->setAlignment(Qt::AlignRight);
    ui->Vlabel2->setAlignment(Qt::AlignRight);
    ui->Vlabel3->setAlignment(Qt::AlignRight);
    ui->Vlabel4->setAlignment(Qt::AlignRight);
    ui->Vlabel5->setAlignment(Qt::AlignRight);

    ui->Vlabel1->setFont((QFont( "lucida", 5, QFont::Normal, TRUE )));
    ui->Vlabel2->setFont((QFont( "lucida", 5, QFont::Normal, TRUE )));
    ui->Vlabel3->setFont((QFont( "lucida", 5, QFont::Normal, TRUE )));
    ui->Vlabel4->setFont((QFont( "lucida", 5, QFont::Normal, TRUE )));
    ui->Vlabel5->setFont((QFont( "lucida", 5, QFont::Normal, TRUE )));

    ui->Vlabel1->setGeometry(0,margin-0.2*size,labelSize,0.5*size);
    ui->Vlabel2->setGeometry(0,((ui->Vline->height()*0.75)+ margin-0.2*size),labelSize,0.5*size);
    ui->Vlabel3->setGeometry(0,((ui->Vline->height()/2.0)+margin-0.2*size),labelSize,0.5*size);
    ui->Vlabel4->setGeometry(0,((ui->Vline->height()/4.0)+margin-0.2*size),labelSize,0.5*size);
    ui->Vlabel5->setGeometry(0.5*size,(ui->Vline->height()+margin-0.2*size),(labelSize-0.5*size),0.4*size);


   plotGraph();

}

GraphMonthly::~GraphMonthly()
{
    delete ui;
}
void GraphMonthly::back()
{
    (new ExpMonth(1,2))->show();
}
void GraphMonthly::plotGraph()
{
    db=QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("finance_db");
    if(!db.open())
    {
        QMessageBox::information(0,"Error !","Error in opning database");

    }
    else
    {
        QDate today;
        today.setDate(year,month,1);
        QDate dayOne,dayLast;
        dayOne.setDate(year,month,1);//First day of year
        dayLast.setDate(year,month,today.daysInMonth());//Last day of year
        qDebug()<<dayLast;
        qDebug()<<dayOne;
        QSqlQuery qry;
        qry.prepare(QString("select * from expense_table where exp_date>=? and exp_date<=?"));
        qry.addBindValue(dayOne);
        qry.addBindValue(dayLast);
        if(qry.exec())
        {
            int list[7]={0,0,0,0,0,0,0};
            QDate dt=dayOne;
            int i=1,j=1,day,count=0;
            while(i<=today.daysInMonth())
            {
                dt.setDate(today.year(),today.month(),i);

                if(dt.dayOfWeek()==7)
                {
                    list[j]=i;
                    qDebug()<<list[j];
                    j++;

                }
                i++;
            }
            int weeks=0;
            qDebug()<<"J"<<j;
            if(list[j-1]==today.daysInMonth())
            {
                weeks=j-1;
            }
            else
                weeks=j;
            if(list[5]==0)
            {
                list[5]=today.daysInMonth();
            }
            if(list[6]==0)
            {
                list[6]=today.daysInMonth();
            }
            double listTot[7]={0,0,0,0,0,0,0};
            while(qry.next())
            {
                day=qry.value(3).toDate().day();
                qDebug()<<"Day"<<day;
                if(day<=list[1])
                    listTot[1]=listTot[1]+qry.value(1).toDouble();
                else if(day>list[1] && day<=list[2])
                    listTot[2]=listTot[2]+qry.value(1).toDouble();
                else if(day>list[2] && day<=list[3])
                    listTot[3]=listTot[3]+qry.value(1).toDouble();
                else if(day>list[3] && day<=list[4])
                    listTot[4]=listTot[4]+qry.value(1).toDouble();
                else if(day>list[4] && day<=list[5])
                    listTot[5]=listTot[5]+qry.value(1).toDouble();
                else if(day>list[5] && day<=list[6])
                    listTot[6]=listTot[6]+qry.value(1).toDouble();
                count++;
            }
            qDebug()<<listTot[1];
            qDebug()<<listTot[2];
            qDebug()<<listTot[3];
            qDebug()<<listTot[4];
            qDebug()<<listTot[5];
            qDebug()<<listTot[6];
            if(count==0)
            {
                ui->label_header->setVisible(true);
                ui->label_header->setText(tr("No expense record found"));
                ui->Vline->setVisible(false);
                ui->Hline->setVisible(false);
                ui->progressBar1->setVisible(false);
                ui->progressBar2->setVisible(false);
                ui->progressBar3->setVisible(false);
                ui->progressBar4->setVisible(false);
                ui->progressBar5->setVisible(false);
                ui->progressBar6->setVisible(false);
                ui->labelX->setVisible(false);


            }
            else
            {
                double max=0;
                for(int i=1;i<=6;i++)
                {
                    if(listTot[i]>max)
                        max=listTot[i];
                }
                ui->progressBar1->setMaximum(max);
                ui->progressBar2->setMaximum(max);
                ui->progressBar3->setMaximum(max);
                ui->progressBar4->setMaximum(max);
                ui->progressBar5->setMaximum(max);
                ui->progressBar6->setMaximum(max);

                ui->progressBar1->setValue(listTot[1]);
                ui->progressBar2->setValue(listTot[2]);
                ui->progressBar3->setValue(listTot[3]);
                ui->progressBar4->setValue(listTot[4]);
                ui->progressBar5->setValue(listTot[5]);
                ui->progressBar6->setValue(listTot[6]);

                ui->label1->setText(tr(" 1"));
                ui->label2->setText(tr(" 2"));
                ui->label3->setText(tr(" 3"));
                ui->label4->setText(tr(" 4"));
                ui->label5->setText(tr(" 5"));
                ui->label6->setText(tr(" 6"));
                if(weeks==4)
                {
                    ui->label5->setVisible(false);
                    ui->label6->setVisible(false);
                }
                else if(weeks==5)
                {
                    ui->label6->setVisible(false);
                }

                int maxi=max;
                qDebug()<<max;
                qDebug()<<maxi;
                ui->Vlabel1->setText(QString("%1").arg(maxi));
                ui->Vlabel2->setText(QString("%1").arg(maxi/4.0));
                ui->Vlabel3->setText(QString("%1").arg(maxi/2.0));
                ui->Vlabel4->setText(QString("%1").arg(maxi*0.75));
                ui->Vlabel5->setText("0");


            }

        }
        db.close();
}

}
